<?php
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new AddressBookAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new AddressBookAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$action=''){
    $s = new AddressBookAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$action);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["stud_no","Student No"],
                   ["firstname","First Name"],
                   ["lastname","Last Name"],
                   ["middlename","Middle Name"],
                   ["type","Type"],
                   ["msisdn","msisdn"]
     //              ["total_loan1","Total Loan"],
       //            ["monthly_payment","Monthly Payment"],
         //          ["balance_loan","Loan Balance"],
           //        ["balance_loan","Loan Balance"],
             //      ["total_cost","Home Beneficiary Total Cost "],
               //    ["monthly_amortization","Monthly Amortization"],
                 //  ["balance_home","Balance "]
		  	
                 ]
    var  extra = [ [true,'id','edit'],
                   [true,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable(data[0],fields, extra)
}

/*function levels_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('level'), 'id','name')
}*/
function clearOrder(a){
  
         data = $('form2').toQueryString();
         $('myid').value=''
//alert("clearing list of cred");

}
function save () {
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
}
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
        $('altfrm').value = "New Contacts"
    } else {
        hide_t('thetable','newform')
        $('altfrm').value = "Back to List"
        $('toolbar-new').disable = "false"
    }
    $('form2').reset();
    $('myid').value = ''
}

function edit (id) {
    x_save('',id,'get', function(new_data1){
       d = Json_eval(new_data1);
       $('stud_no').value = d[0]['stud_no'];
       $('firstname').value = d[0]['firstname'];
       $('lastname').value = d[0]['lastname'];
       $('middlename').value = d[0]['middlename'];
       $('email_add').value = d[0]['email_add'];
       $('no').value = d[0]['add_no'];
       $('street').value = d[0]['add_street'];
       $('subd').value = d[0]['add_subdbrgy'];
       $('town').value = d[0]['add_townctry'];
       $('zip').value = d[0]['add_zipcode'];
       $('homenumber').value = d[0]['homenumber'];
       $('type').value = d[0]['type'];
    //   $('total_loan').value = d[0]['total_loan1'];
    //   $('balance_loan').value = d[0]['balance_loan'];
    //   $('monthlypayment').value = d[0]['monthly_payment'];
    //   $('total_cost').value = d[0]['total_cost'];
    //   $('monthly_amortization').value = d[0]['monthly_amortization'];
    //   $('balance_home').value = d[0]['balance_home'];
       $('msisdn').value = d[0]['msisdn'];
       $('myid').value = d[0]['id'];
       //optionSelected($('level'), d[0]['level_id'])
       hide_t('thetable','newform');
    }) 
    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}


window.addEvent('domready', function() {
$$('.hasTip').each(function(el) {
  var title = el.get('title');
  if (title) {
    var parts = title.split('::', 2);
    el.store('tip:title', parts[0]);
    el.store('tip:text', parts[1]);
  }
});
var JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: false});
});
</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<div id="toolbar-box">
  <div class="m">
    <div class="toolbar-list" id="toolbar">
      <!-- TOOL BAR MENU ex:HELP|NEW|EDIT-->
      <ul>
      <!--  <li class="button" id="toolbar-cancel"><a href="#" onclick="show_t()" class="toolbar"><span class="icon-32-cancel"></span>Cancel</a></li>-->
      </ul>
      <!-- END TOOL BAR MENU -->
      <div class="clr"></div>
    </div>
    <div class="pagetitle icon-48-cpanel"><h2><?=$config['version']?></h2></div>
     <!-- [</?=strtoupper($auth['privilege'])?>]-->
  </div>
</div>
<div>
 <span><h2>&nbsp;&nbsp;&nbsp;::ADDRESS BOOK::</h2><span>
</div>
<div id="submenu-box">
  <div class="m">
    <div class="width-100 fltlft">
      <fieldset class="adminform">
        <legend><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></legend>
       <!-- filter panel -->
        <div id="panel">
          <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
            <span><label class="desc">Seach Contact:</label></span>
            <select id="jform_params_helpsite" name="search[searchfield]">
	      <option value="lastname" selected="selected">-Use Default-</option>
	      <option value="stud_no">Ref. No</option>
              <option value="firstname">First Name</option>
              <option value="lastname">Last Name</option>
              <option value="middlename">Middle Name</option>
              <option value="msisdn">Mobile Number</option>
              <option value="add_no">Street No.</option>
              <option value="add_street">Street</option>
              <option value="add_subdbrgy">Subdivision/Brgy</option>
              <option value="add_townctry">Town/City</option>
              <option value="add_zipcode">Zip Code</option>
              <option value="type">Type</option>
            </select>
            <input type="text" name="search[searchtext]" value="" class="inputbox required" size="30"onKeyup="filter()"/>
            <button id="saveForm" class="btn" onClick="filter()">Search</button>
            <button class="btn" onClick="this.form.reset();filter()">Clear</button>
            <button id="altfrm" class="btn" type="button" value="New Student" onClick="show_t()"/>New Contact</button>
          </form>
          <form enctype="multipart/form-data" action="" method="POST">
          <!--<input type="hidden" name="MAX_FILE_SIZE" value="<?=ini_get('upload_max_filesize')?>" />-->
            <span><label class="desc">Choose a file to upload:</label></span>
            <input name="uploadedfile" type="file" />
            <button type="submit" name="import" class="btn">Upload File</button>
            <button type="submit" name="export" class="btn">Export File</button>
          </form>
        </div>
      </fieldset>
      <!-- filter panel end -->
    </div>
    <div class="clr"></div>
  </div>
</div>
<div><h2>:: SEARCH RESULTS</h2></div>
<div id="element-box">
<form id="form2" method="post" onSubmit="return false;">
  <div class="m">
    <div id="thetable"><img src="/mcs/modpub/images/loading_small.gif"/></div>
    <div id="newform" style="display: none">
    <input type="hidden" value="" id="myid" />
      <div class="width-35 fltlft">
        <fieldset class="adminform">
        <legend>My Profile Details</legend>
        <ul class="adminformlist">
          <li><span><label class="desc">Referece No.(ID)</span></label>
            <input type="text" name="details[stud_no]" id="stud_no" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Last Name</span></label>
            <input type="text" name="details[lastname]" id="lastname" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">First Name</span></label>
            <input type="text" name="details[firstname]" id="firstname" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Middle Name</span></label>
            <input type="text" name="details[middlename]" id="middlename" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Email Address</span></label>
            <input type="text" name="details[email_add]" id="email_add" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Type</span></label>
            <select class="field select" style="width:10em" id="type" name="details[type]">
              <option value="">N/A</option>
              <option value="STUDENT">Student</option>
              <option value="EMPLOYEE">Employee</option>
            </select>
          </li>
        </ul>
        </fieldset>
      </div>
      <div class="width-65 fltrt">
        <div>
          <div style="display:none;"><div>
        </div>
      </div>
      <div class="width-10 fltlft">
        <fieldset class="adminform">
        <legend>Address</legend>
        <ul class="adminformlist">
          <li><span><label class="desc">No.</span></label>
            <input type="text" name="details[add_no]" id="no" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Street</span></label>
            <input type="text" name="details[add_street]" id="street" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Subdivision/Brgy</span></label>
            <input type="text" name="details[add_subdbrgy]" id="subd" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Town/ City</span></label>
            <input type="text" name="details[add_townctry]" id="town" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Zip Code</span></label>
            <input type="text" name="details[add_zipcode]" id="zip" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Telephone No.</span></label>
            <input type="text" name="details[homenumber]" id="homenumber" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">Mobile No.</span></label>
            <input type="text" name="details[msisdn]" id="msisdn" value="" class="inputbox" size="30"/>
          </li>
        </ul>
        </fieldset>
        <ul>
          <li><input id="saveForm" class="btTxt" type="button" value="Save" onClick="save();"/>
            <input id="saveForm" class="btTxt" type="button" value="Clear" onClick="this.form.reset();clearOrder();"/>
            <input id="saveForm" class="btTxt" type="button" value="Cancel" onClick="this.form.reset();clearOrder();hide_t('newform', 'thetable')"/>
          </li>
        </ul>
      </div>
    </div>
    </div>
    <div class="clr"></div>
  </div>
</div>
</div>
</div>
</form>
</br>
<script type="text/javascript">
x_getlist(getlist_cb)
</script>
<?php require('footer.php'); ?>
</body>
</html>
